# -*- mode: makefile -*-
#
# Mongoose OS Firmware boot loader for CC3200
#
MAKEFLAGS += --no-builtin-rules --warn-undefined-variables

override APP = mg-boot
override APP_PLATFORM = cc3200
override APP_MODULES = src lib
CREATE_ZIP ?= 1

SDK_PATH ?= /cc3200-sdk
MGOS_PATH ?= ../../../..
BUILD_DIR ?= $(CURDIR)/.build
FW_DIR ?= $(CURDIR)/firmware

include $(MGOS_PATH)/tools/mk/mgos_common.mk

VPATH = $(APP_MODULES) $(BUILD_DIR)
IPATH = $(APP_MODULES) $(BUILD_DIR) $(MGOS_PATH) $(MGOS_PATH)/include $(MGOS_PATH)/platforms/cc3200/src

SDK_FLAGS = -DTARGET_IS_CC3200

APP_ELF = $(BUILD_DIR)/$(APP).axf
APP_BIN = $(FW_DIR)/$(APP).bin
MKCFG_BIN = $(BUILD_DIR)/mkcfg
BUILD_INFO_C = $(BUILD_DIR)/build_info.c
BUILD_INFO_O = $(BUILD_DIR)/build_info.o
BUILD_INFO_JSON = $(BUILD_DIR)/build_info.json

.PHONY: all clean flash

DRIVER_SRCS = cpu.c flash.c interrupt.c pin.c prcm.c spi.c timer.c uart.c utils.c
SL_SRCS = cc_pal.c device.c driver.c fs.c nonos.c socket.c netapp.c
SDK_SRCS = $(DRIVER_SRCS) $(SL_SRCS)
IPATH += $(SDK_PATH) $(SDK_PATH)/inc $(SDK_PATH)/driverlib $(SDK_PATH)/oslib \
         $(SDK_PATH)/simplelink $(SDK_PATH)/simplelink/include \
         $(SDK_PATH)/simplelink_extlib/provisioninglib
VPATH += $(SDK_PATH)/driverlib $(SDK_PATH)/oslib $(SDK_PATH)/simplelink \
         $(SDK_PATH)/example/common \
         $(SDK_PATH)/simplelink/source

APP_SRCS := $(notdir $(foreach m,$(APP_MODULES),$(wildcard $(m)/*.c)))
SRCS = $(APP_SRCS) $(SDK_SRCS)

.DEFAULT_GOAL := all

clean:
	@rm -rf $(BUILD_DIR) $(wildcard *~)
	@rm -rf $(FW_DIR) $(wildcard *~)
	@rm -f $(MKCFG_BIN)

$(BUILD_DIR) $(FW_DIR):
	$(vecho) "MKDIR $@"
	@mkdir -p $@

ifeq "$(CREATE_ZIP)" "1"
FW_PARTS = /sys/mcuimg.bin:src=$(notdir $(APP_BIN))
include $(MGOS_PATH)/tools/mk/mgos_fw_meta.mk
endif

OBJS = $(addprefix $(BUILD_DIR)/,$(patsubst %.c,%.o,$(SRCS)))

include $(MGOS_PATH)/tools/mk/mgos_build_info.mk

SDK_OBJS = $(addprefix $(BUILD_DIR)/,$(patsubst %.c,%.o,$(SDK_SRCS)))

OBJCOPY = arm-none-eabi-objcopy

include $(MGOS_PATH)/platforms/cc3200/gcc.mk

CFLAGS += $(patsubst %,-I%,$(subst :, ,$(IPATH)))
CFLAGS += -DNDEBUG -DCS_NDEBUG -DCS_PLATFORM=CS_P_CC3200 \
          -DMGOS_DEBUG_UART=$(MGOS_DEBUG_UART) \
          -DMGOS_DEBUG_UART_BAUD_RATE=$(MGOS_DEBUG_UART_BAUD_RATE) \
          $(SDK_FLAGS) $(CFLAGS_EXTRA)

dirs: $(BUILD_DIR) $(FW_DIR)
all: dirs $(BUILD_INFO_C) $(MKCFG_BIN)

ifeq "$(CREATE_ZIP)" "1"
all: $(FW_ZIP)
$(FW_MANIFEST): $(APP_BIN)
else
all: $(APP_BIN)
endif

$(APP_BIN): $(APP_ELF)
	$(vecho) "BIN   $< -> $@"
	$(Q) $(OBJCOPY) -O binary $< $@
	$(Q) $(OBJCOPY) -O binary --only-section=.relocator $< $@.reloc.bin
	$(Q) $(OBJCOPY) -O binary --remove-section=.relocator $< $@.main.bin
	$(Q) cat $@.reloc.bin $@.main.bin > $@
	$(Q) rm $@.reloc.bin $@.main.bin
	@echo "    Boot code size: $$(ls -l $@ | awk '{print $$5}')"

$(APP_ELF): $(OBJS) $(BUILD_INFO_O)
	$(call link,src/boot.ld,--entry=ResetISR,$(OBJS) $(BUILD_INFO_O))

$(BUILD_DIR)/%.o: %.c
	$(call cc,$(CFLAGS),$<)

$(BUILD_DIR)/%.o: %.asm
	$(call asm,$(CFLAGS),$<)

$(BUILD_INFO_C) $(BUILD_INFO_JSON): $(OBJS)
	$(call gen_build_info,$@,,,,,$(BUILD_INFO_C),$(BUILD_INFO_JSON))

$(BUILD_INFO_O): $(BUILD_INFO_C)

$(MKCFG_BIN): tools/mkcfg.c lib/boot_meta.h
	$(vecho) "GCC   $<"
	$(Q) gcc -Wall -I$(MGOS_PATH)/platforms/cc3200/src -Werror -o $@ $^
